{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-21T09:19:13.937531900Z",
     "start_time": "2024-02-21T09:19:13.267899500Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "\n",
    "from advisor_backend.interface import Interface\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 算子调优分析\n",
    "## 1. 算子分析的数据准备\n",
    "当前算子分析工具支持分析Ascend Pyorch Profiler方式生成的ascend_pt目录\n",
    "## 2. 融合算子分析\n",
    "当前支持分析模型中存在可融合的小算子，并给出优化建议。\n",
    "\n",
    "\"更多融合算子信息，请查阅 https://www.hiascend.com/document/detail/zh/CANNCommunityEdition/700alpha003/processormodel/hardwaredesc_0001.html\n",
    "\n",
    "## 3. 异常性能算子分析\n",
    "支持分析模型中性能异常的计算算子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-22T08:41:17.455567500Z",
     "start_time": "2024-02-22T08:41:16.716884800Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[INFO] Start to analyse the target file: D:\\work\\ascend_pt\\ASCEND_PROFILER_OUTPUT\\kernel_details.csv\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pattern_name</th>\n",
       "      <th>pattern</th>\n",
       "      <th>len</th>\n",
       "      <th>count</th>\n",
       "      <th>duration sum(us)</th>\n",
       "      <th>op durations(us)</th>\n",
       "      <th>index</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>torch_npu.npu_swiglu</td>\n",
       "      <td>(Slice, Slice, Swish, Mul)</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>27.53</td>\n",
       "      <td>[21.2, 0.05, 3.14, 3.14]</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            pattern_name                     pattern  len  count  duration sum(us)          op durations(us) index\n",
       "18  torch_npu.npu_swiglu  (Slice, Slice, Swish, Mul)    4      1             27.53  [21.2, 0.05, 3.14, 3.14]   [0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "The computing time of fusable op is 27.53 ms.\n",
      "\n",
      "\n",
      "Advice 0:\n",
      "Replace [Slice, Slice, Swish, Mul] with torch_npu.npu_swiglu. This pattern first happened in: \n",
      "/root/torch/module.py\n",
      "/root/test/slice.py(116)\n"
     ]
    }
   ],
   "source": [
    "# EDIT THE PROFILING DATA PATH\n",
    "compute_path = \"[YOUR PATH]\"\n",
    "interface = Interface(compute_path)\n",
    "data = interface.get_data('compute', 'npu_fused')\n",
    "pd.set_option('display.max_columns', None)\n",
    "pd.set_option('display.width', 900)\n",
    "display(data['data'].iloc[:, :-2])\n",
    "print('\\n')\n",
    "print(data['bottleneck'])\n",
    "print('\\n')\n",
    "print(data['advice'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[INFO] Start to analyse the target file: D:\\work\\ascend_pt\\ASCEND_PROFILER_OUTPUT\\kernel_details.csv\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Step Id</th>\n",
       "      <th>Model ID</th>\n",
       "      <th>Task ID</th>\n",
       "      <th>Stream ID</th>\n",
       "      <th>Name</th>\n",
       "      <th>Type</th>\n",
       "      <th>Accelerator Core</th>\n",
       "      <th>Start Time(us)</th>\n",
       "      <th>Duration(us)</th>\n",
       "      <th>Wait Time(us)</th>\n",
       "      <th>Block Dim</th>\n",
       "      <th>Mix Block Dim</th>\n",
       "      <th>Input Shapes</th>\n",
       "      <th>Input Data Types</th>\n",
       "      <th>Input Formats</th>\n",
       "      <th>Output Shapes</th>\n",
       "      <th>Output Data Types</th>\n",
       "      <th>Output Formats</th>\n",
       "      <th>Context ID</th>\n",
       "      <th>aicore_time(us)</th>\n",
       "      <th>aic_total_cycles</th>\n",
       "      <th>aic_mac_ratio</th>\n",
       "      <th>aic_mac_int8_ratio</th>\n",
       "      <th>aic_cube_fops</th>\n",
       "      <th>aic_vector_fops</th>\n",
       "      <th>aiv_time(us)</th>\n",
       "      <th>aiv_total_cycles</th>\n",
       "      <th>aiv_vec_fp32_ratio</th>\n",
       "      <th>aiv_vec_fp16_ratio</th>\n",
       "      <th>aiv_vec_int32_ratio</th>\n",
       "      <th>aiv_vec_misc_ratio</th>\n",
       "      <th>aiv_cube_fops</th>\n",
       "      <th>aiv_vector_fops</th>\n",
       "      <th>size(MB)</th>\n",
       "      <th>throughput(GB/s)</th>\n",
       "      <th>color</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>4294967295</td>\n",
       "      <td>1265</td>\n",
       "      <td>16</td>\n",
       "      <td>Slice1</td>\n",
       "      <td>Slice</td>\n",
       "      <td>AI_VECTOR_CORE</td>\n",
       "      <td>1699529623106750</td>\n",
       "      <td>21.20</td>\n",
       "      <td>261.56</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>4,1025</td>\n",
       "      <td>INT64</td>\n",
       "      <td>FORMAT_ND</td>\n",
       "      <td>4,1025</td>\n",
       "      <td>INT32</td>\n",
       "      <td>FORMAT_ND</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.77</td>\n",
       "      <td>29508.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0062</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5856.0</td>\n",
       "      <td>0.046921</td>\n",
       "      <td>2.161371</td>\n",
       "      <td>RED</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>4294967295</td>\n",
       "      <td>1265</td>\n",
       "      <td>16</td>\n",
       "      <td>Add1</td>\n",
       "      <td>Add</td>\n",
       "      <td>AI_CORE</td>\n",
       "      <td>1699529623106754</td>\n",
       "      <td>3.14</td>\n",
       "      <td>261.56</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>4,1025</td>\n",
       "      <td>INT64</td>\n",
       "      <td>FORMAT_ND</td>\n",
       "      <td>4,1025</td>\n",
       "      <td>INT32</td>\n",
       "      <td>FORMAT_ND</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.3</td>\n",
       "      <td>28888.0</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.7</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.046921</td>\n",
       "      <td>14.592698</td>\n",
       "      <td>RED</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Step Id    Model ID  Task ID  Stream ID    Name   Type Accelerator Core    Start Time(us)  Duration(us)  Wait Time(us)  Block Dim  Mix Block Dim Input Shapes Input Data Types Input Formats Output Shapes Output Data Types Output Formats  Context ID  aicore_time(us)  aic_total_cycles  aic_mac_ratio  aic_mac_int8_ratio  aic_cube_fops  aic_vector_fops  aiv_time(us)  aiv_total_cycles  aiv_vec_fp32_ratio  aiv_vec_fp16_ratio  aiv_vec_int32_ratio  aiv_vec_misc_ratio  aiv_cube_fops  aiv_vector_fops  size(MB)  throughput(GB/s) color\n",
       "0        1  4294967295     1265         16  Slice1  Slice   AI_VECTOR_CORE  1699529623106750         21.20         261.56          9              0       4,1025            INT64     FORMAT_ND        4,1025             INT32      FORMAT_ND         NaN              0.0               0.0            0.0                 0.0            0.0              0.0          1.77           29508.0                 0.0                 0.0               0.0062                 0.0            0.0           5856.0  0.046921          2.161371   RED\n",
       "4        1  4294967295     1265         16    Add1    Add          AI_CORE  1699529623106754          3.14         261.56          9              0       4,1025            INT64     FORMAT_ND        4,1025             INT32      FORMAT_ND         NaN              2.3           28888.0            0.2                 0.1            0.1              0.7          0.00               0.0                 0.0                 0.0               0.0000                 0.0            0.0              0.0  0.046921         14.592698   RED"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 异常性能算子识别\n",
    "from advisor_backend.compute_advice.npu_slow_advice import NpuSlowAdvice\n",
    "\n",
    "npu_slow_advice = NpuSlowAdvice(compute_path)\n",
    "data = interface.get_data('compute', 'npu_slow')\n",
    "slow_op_data = data[data[\"color\"] == \"RED\"]\n",
    "display(slow_op_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "NpuSlowAdvice.save_to_excel(data, file_path=os.path.join(compute_path, \"slow_op.xlsx\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "call stack: \n",
      "/root/torch/module.py\n",
      "/root/test/slice.py(116)\n"
     ]
    }
   ],
   "source": [
    "# 异常性能算子call stack\n",
    "call_stack = npu_slow_advice.get_call_stack(data, index_id=0, ts_col=\"Start Time(us)\")\n",
    "print(\"call stack: \")\n",
    "print(call_stack)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
